package com.example.callmanagerandroid;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.database.Contact;

public class QuanLiDauSo extends Activity implements OnItemClickListener{
	
	private ListView list;
	private ImageView btn_add;
	private EditText edt;
	private AlertDialog dialog;
	private ArrayList<String> phone_list= new ArrayList<String>(); 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.quan_ly_dau_so_tin_nhan);
		list = (ListView) findViewById(R.id.listView1);
		btn_add = (ImageView) findViewById(R.id.imageView1);
		edt = (EditText) findViewById(R.id.editText1);
		 
		btn_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Contact contact = new Contact(QuanLiDauSo.this);
				if(!edt.getText().toString().equals("")){
					contact.insertPhone(edt.getText().toString());
					contact.close();
					contact = null;
					reloadComment();
				}
			}
		});
		
		reloadComment();
		
		
		
		list.setOnItemClickListener(this);
	}
	
	private void reloadComment(){
		phone_list = new ArrayList<String>();
		Contact contact = new Contact(this);
		Cursor c = contact.selectPhone();
		if(c!=null)
			if(c.moveToFirst()){
				do {
					String number = c.getString(c.getColumnIndexOrThrow("number"));
					phone_list.add(number);
				} while (c.moveToNext());
				list.setAdapter(new DauSoAdapter(phone_list, QuanLiDauSo.this));
			}
		c = null;
		contact.close();
		contact = null;
	}
	
	private void createDialodAndShow(final String number){
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				QuanLiDauSo.this);
 
			alertDialogBuilder.setTitle("Chú ý");
			alertDialogBuilder
				.setMessage("Bạn có muốn xóa số điện thoại : "+number)
				.setCancelable(false)
				.setPositiveButton("có",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						Contact contact = new Contact(QuanLiDauSo.this);
						contact.deleteNumber(number);
						contact.close();
						contact = null;
						dialog.dismiss();
					}
				  })
				.setNegativeButton("Không",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						dialog.dismiss();
					}
				});
				dialog = alertDialogBuilder.create();
				dialog.show();
				dialog.setOnDismissListener(new OnDismissListener() {
					
					@Override
					public void onDismiss(DialogInterface arg0) {
						reloadComment();
					}
				});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		TextView text = (TextView) arg1.findViewById(R.id.textView1);
		createDialodAndShow(text.getText().toString());
		
	}

}
